package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-09
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;//注入小结的service
    //课程大纲列表  根据课程id查询
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1.根据课程id 查询 课程的所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
           //传入条件
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //2.根据课程id 查询课程里面的所有小结
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        //传入条件
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);


        List<ChapterVo> finalList= new ArrayList<>();
        //3.遍历查询章节list 集合进行封装
        for (int i = 0; i < eduChapterList.size(); i++) {

            EduChapter eduChapter = eduChapterList.get(i);
            //eduChapter -->finalList
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);

            finalList.add(chapterVo);

            //创建集合 封装章节 的小结
            List<VideoVo>  videoVoList = new ArrayList<>();

            //4.遍历查询的小结 list 进行封装
            for (int m = 0; m < eduVideoList.size(); m++) {

                //
                EduVideo eduVideo = eduVideoList.get(m);
                //判断 小结的chapter_id 得和 chapter的 id 一样
                if(eduVideo.getChapterId().equals(eduChapter.getId())){

                    VideoVo videoVo= new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);

                    //放入list集合
                    videoVoList.add(videoVo);
                }

            }
            //小结集合放入 章节对象里面去
            chapterVo.setChildren(videoVoList);
        }
        return finalList;
    }

    //删除章节
    @Override
    public boolean deleteChapter(String chapterId) {

        //根据Chapter -id 在小结表中查询 查询到就不删除 没有就删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();

        wrapper.eq("chapter_id",chapterId);

        System.out.println("chapterId"+chapterId);

        int count = videoService.count(wrapper);

        if(count>0){
            //进行删除
            throw  new GuliException(20001,"不能删除");
        }else{
            //删除 不能查询数据，进行删除
//            System.out.println("执行了删除");
            int result = baseMapper.deleteById(chapterId);
//            System.out.println("返回值"+result);

            return result>0;
        }

    }
    //根据课程id 删除章节
    @Override
    public void removeChapterBycourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);

    }
}
